﻿using Abp.Domain.Entities;
using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace AbpProjectTemplate.Extensions
{
    public static class IRepositoryExtensions
    {
        /// <summary>
        /// 返回纯查询使用的IQueryable，使用EF CORE的AsNoTracking
        /// </summary>
        public static IQueryable<TEntity> GetAllAsNoTracking<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository)
            where TEntity : class, IEntity<TPrimaryKey>
        {
            return repository.GetAll().AsNoTracking();
        }
        /// <summary>
        /// 是否存在指定条件的数据
        /// </summary>
        public static async Task<bool> AnyAsync<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, Expression<Func<TEntity, bool>> predicate)
            where TEntity : class, IEntity<TPrimaryKey>
        {
            return await repository.GetAll().AnyAsync(predicate);
        }
        /// <summary>
        /// 是否不存在指定条件的数据
        /// </summary>
        public static async Task<bool> NotExistsAsync<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, Expression<Func<TEntity, bool>> predicate)
            where TEntity : class, IEntity<TPrimaryKey>
        {
            return !await repository.GetAll().AnyAsync(predicate);
        }
    }
}
